<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="CAdditionalRequestForm">
          <a-row>
              <a-divider >重要信息</a-divider >
						<a-col :span="8" >
							<a-form-item label="项目"  v-bind="validateInfos.projectId" id="CAdditionalRequestForm-projectId" name="projectId">
								 <j-dict-select-tag showSearch="true" v-model:value="formData.projectId" dictCode="c_project_application,project_name,id" placeholder="请选择项目"  allow-clear />
							</a-form-item>
						</a-col>
            	<a-col :span="8">
							<a-form-item label="标题" v-bind="validateInfos.title" id="CAdditionalRequestForm-title" name="title">
								<a-input v-model:value="formData.title" placeholder="请输入标题"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
            <a-col :span="8">
							<a-form-item label="项目编号" v-bind="validateInfos.projectNumber" id="CAdditionalRequestForm-projectNumber" name="projectNumber">
								<a-input v-model:value="formData.projectNumber" placeholder="请输入项目编号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
            <a-col :span="8">
							<a-form-item label="项目名称" v-bind="validateInfos.projectName" id="CAdditionalRequestForm-projectName" name="projectName">
								<a-input v-model:value="formData.projectName" placeholder="请输入项目名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
             	<a-col :span="8">
							<a-form-item label="期望完成日期" v-bind="validateInfos.expectFulfillData" id="CAdditionalRequestForm-expectFulfillData" name="expectFulfillData">
								<a-date-picker placeholder="请选择期望完成日期"  v-model:value="formData.expectFulfillData" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
            	<a-col :span="8">
							<a-form-item label="优先级" v-bind="validateInfos.priority" id="CAdditionalRequestForm-priority" name="priority">
								<a-input v-model:value="formData.priority" placeholder="请输入优先级"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
            	<a-col :span="8">
							<a-form-item label="预估费用" v-bind="validateInfos.purchaseEstimatedCost" id="CAdditionalRequestForm-purchaseEstimatedCost" name="purchaseEstimatedCost">
								<a-input-number v-model:value="formData.purchaseEstimatedCost" placeholder="请输入预估费用" style="width: 100%" />
							</a-form-item>
						</a-col>
            	<a-col :span="8">
							<a-form-item label="仓库" v-bind="validateInfos.warehouse" id="CAdditionalRequestForm-warehouse" name="warehouse">
								<a-input v-model:value="formData.warehouse" placeholder="请输入仓库"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
            <a-col :span="8">
							<a-form-item label="申请日期" v-bind="validateInfos.applicationDate" id="CAdditionalRequestForm-applicationDate" name="applicationDate">
								<a-date-picker placeholder="请选择申请日期"  v-model:value="formData.applicationDate" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
            	<a-col :span="8">
							<a-form-item label="需求附件" v-bind="validateInfos.appendix" id="CAdditionalRequestForm-appendix" name="appendix">
								<j-upload v-model:value="formData.appendix" dragger="true"  :show-upload-list="{ showDownloadIcon: true, showRemoveIcon: true }"  ></j-upload>
							</a-form-item>
						</a-col>
            	<a-col :span="8">
							<a-form-item label="申请原因" v-bind="validateInfos.applicationReason" id="CAdditionalRequestForm-applicationReason" name="applicationReason">
								<a-textarea v-model:value="formData.applicationReason" :rows="4" placeholder="请输入申请原因" />
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-form-item label="申请内容" v-bind="validateInfos.applicationContent" id="CAdditionalRequestForm-applicationContent" name="applicationContent">
								<a-textarea v-model:value="formData.applicationContent" :rows="4" placeholder="请输入申请内容" />
							</a-form-item>
						</a-col>
             <a-divider >项目</a-divider >




						<a-col :span="8">
							<a-form-item label="申请人" v-bind="validateInfos.applicant" id="CAdditionalRequestForm-applicant" name="applicant">
								<a-input v-model:value="formData.applicant" placeholder="请输入申请人"  allow-clear ></a-input>
							</a-form-item>
						</a-col>

						<a-col :span="8">
							<a-form-item label="所属部门" v-bind="validateInfos.department" id="CAdditionalRequestForm-department" name="department">
								<a-input v-model:value="formData.department" placeholder="请输入所属部门"  allow-clear ></a-input>
							</a-form-item>
						</a-col>

						<a-col :span="8">
							<a-form-item label="需求类型" v-bind="validateInfos.requestType" id="CAdditionalRequestForm-requestType" name="requestType">
								<a-input v-model:value="formData.requestType" placeholder="请输入需求类型"  allow-clear ></a-input>
							</a-form-item>
						</a-col>


						<a-col :span="8">
							<a-form-item label="预计完成日期" v-bind="validateInfos.predictFulfillData" id="CAdditionalRequestForm-predictFulfillData" name="predictFulfillData">
								<a-date-picker placeholder="请选择预计完成日期"  v-model:value="formData.predictFulfillData" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>


						<a-col :span="8">
							<a-form-item label="采购品牌型号" v-bind="validateInfos.purchaseModel" id="CAdditionalRequestForm-purchaseModel" name="purchaseModel">
								<a-input v-model:value="formData.purchaseModel" placeholder="请输入采购品牌型号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-form-item label="采购数量" v-bind="validateInfos.purchaseQuantity" id="CAdditionalRequestForm-purchaseQuantity" name="purchaseQuantity">
								<a-input-number v-model:value="formData.purchaseQuantity" placeholder="请输入采购数量" style="width: 100%" />
							</a-form-item>
						</a-col>






             <a-divider >其他信息</a-divider >
            	<a-col :span="8">
							<a-form-item label="直接维修" v-bind="validateInfos.directMaintenance" id="CAdditionalRequestForm-directMaintenance" name="directMaintenance">
								<a-input v-model:value="formData.directMaintenance" placeholder="请输入直接维修"  allow-clear ></a-input>
							</a-form-item>
						</a-col>

						<a-col :span="8">
							<a-form-item label="审核备注" v-bind="validateInfos.processingNotes" id="CAdditionalRequestForm-processingNotes" name="processingNotes">
								<a-textarea v-model:value="formData.processingNotes" :rows="4" placeholder="请输入审核备注" />
							</a-form-item>
						</a-col>
             <a-col :span="8">
                     <a-form-item label="维修/维保服务验收要求" v-bind="validateInfos.acceptingClaim" id="CAdditionalRequestForm-acceptingClaim" name="acceptingClaim">
                      <a-textarea v-model:value="formData.acceptingClaim" :rows="4" placeholder="请输入维修/维保服务验收要求" />
                    </a-form-item>
             </a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../CAdditionalRequest.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    projectId: '',
    processingNotes: '',   
    title: '',   
    projectNumber: '',   
    applicant: '',   
    applicationDate: '',   
    department: '',   
    projectName: '',   
    requestType: '',   
    expectFulfillData: '',   
    priority: '',   
    warehouse: '',   
    predictFulfillData: '',   
    directMaintenance: '',   
    purchaseModel: '',   
    purchaseQuantity: undefined,
    purchaseEstimatedCost: undefined,
    applicationReason: '',   
    applicationContent: '',   
    acceptingClaim: '',   
    appendix: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    // projectId: [{ required: true, message: '请输入项目ID!'},],
    // title: [{ required: true, message: '请输入标题!'},],
    // projectNumber: [{ required: true, message: '请输入项目编号!'},],
    // applicant: [{ required: true, message: '请输入申请人!'},],
    // applicationDate: [{ required: true, message: '请输入申请日期!'},],
    // department: [{ required: true, message: '请输入所属部门!'},],
    // projectName: [{ required: true, message: '请输入项目名称!'},],
    // requestType: [{ required: true, message: '请输入需求类型!'},],
    // expectFulfillData: [{ required: true, message: '请输入期望完成日期!'},],
    // priority: [{ required: true, message: '请输入优先级!'},],
    // warehouse: [{ required: true, message: '请输入仓库!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add(record) {
    edit(record);
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
